package Server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

class ServerConnection implements Runnable {
	BufferedReader is;
	PrintStream os;
	Socket clientSocket;
	int id;
	String banknumber = "BNK5397";
	Server server;
	String response;
	boolean serverStop = false;
	Account account = null;

	public ServerConnection(Socket clientSocket, int id, Server server) {
		this.clientSocket = clientSocket;
		this.id = id;
		this.server = server;
		System.out.println("Connection " + id + " established with: "
				+ clientSocket);
		try {
			is = new BufferedReader(new InputStreamReader(
					clientSocket.getInputStream()));
			os = new PrintStream(clientSocket.getOutputStream());
		} catch (IOException e) {
			System.out.println(e);
		}
	}

	public void run() {

		String line;
		try {

			while (serverStop == false) {
				line = is.readLine();
				System.out.println("Received " + line + " from Connection "
						+ id + ".");
				if (line != null && !line.isEmpty()){
					determineAction(line);
					System.out.println(response);
					os.println(response);
				}
			}
			System.out.println("Connection " + id + " closed.");
			is.close();
			os.close();
			clientSocket.close();
			server.stopServer();
		} catch (IOException e) {
			System.out.println(e);
		}
	}

	private void determineAction(String line) {
		Command command = null;
		String[] parts = line.split(" ");
		// Command command = Command.valueOf(parts[0]);
		for (Command c : Command.values()) {
			if (c.getType().equals(parts[0])) {
				command = c;
			}
		}
		switch (command) {
		case CONNECT:
			verifyATM(line);
			break;
		case OK:
			verifyConnected();
			break;
		case FAIL:
			disconnect();
			break;
		case LOGIN:
			verifyLogin(line);
			break;
		case WITHRAW:
			verifyWithdraw(line);
			break;
		case REQUEST:
			notifyAmount();
			break;
		case LOGOUT:
			logout();
			break;
		case EXIT:
			disconnect();
			break;
		default:
			response = "";
			break;
		}
	}

	private void logout() {
		account = null;
		response = "250 logged_out";
	}

	private void notifyAmount() {
		response = "240 " + account.balance();
		System.out.println("the balance is: "+ account.balance());
	}

	private void verifyWithdraw(String line) {
		String[] parts = line.split(" ");
		int amount = Integer.parseInt(parts[1]);
		if (account.withdraw(amount) >= 0) {
			response = "230 "+ amount;
		} else {
			response = "231 Not_enough_money";
		}
	}

	private void verifyLogin(String line) {
		String[] parts = line.split(" ");
		String passnumber = parts[1];
		String pincode = parts[2];
		for (Account account : server.getAccounts()) {
			if (account.getPassnumber() == Integer.parseInt(passnumber)) {
				if (account.getPincode() == Integer.parseInt(pincode)) {
					this.account = account;
					System.out.println("BALANCE: " + account.balance());
					response = "220 Access_granted";
				} else {
					response = "505 Error_in_line "+ line;
				}
			}
		}
	}

	private void disconnect() {
		response = "666 exit_program";
		serverStop = true;
	}

	private void verifyConnected() {
		response = "210 Connected";
	}

	private void verifyATM(String line) {
		String[] parts = line.split(" ");
		String ATM = parts[1];
		String ATMPassword = parts[2];
		System.out.println(ATM + " " + ATMPassword);
		if (ATM.equals("ATM4286") && ATMPassword.equals("9876")) {
			response = "310 Access_granted BNK5397";
		} else{
			response = "505 Error_in_line "+ line;
		}
	}
}